package com.ala4.oxcafe.repository;

import com.ala4.oxcafe.boot.page.PageDomain;
import com.ala4.oxcafe.common.utils.collection.CollUtil;
import com.ala4.oxcafe.common.utils.object.ObjectUtil;
import com.ala4.oxcafe.common.utils.text.StrUtil;
import com.ala4.oxcafe.entity.RoleInfo;
import com.ala4.oxcafe.entity.UserInfo;
import com.ala4.oxcafe.exception.BusinessException;
import com.ala4.oxcafe.repository.mapper.UserInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户信息Repository
 *
 * @author PING
 * @date 2025-03-25
 */
@Repository
@AllArgsConstructor
public class UserInfoRepository extends ServiceImpl<UserInfoMapper, UserInfo> implements IService<UserInfo> {

    private final PasswordEncoder passwordEncoder;

    public IPage<UserInfo> selectUserInfoPage(PageDomain pageDomain, UserInfo userInfo) {

        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(StrUtil.isNotEmpty(userInfo.getSearchValue()), item -> {
            item.like(UserInfo::getPhoneNumber, userInfo.getSearchValue())
                    .or().like(UserInfo::getUserAccount, userInfo.getSearchValue())
                    .or().like(UserInfo::getNickName, userInfo.getSearchValue());
        });
        queryWrapper.eq(ObjectUtil.isNotNull(userInfo.getStatus()), UserInfo::getStatus, userInfo.getStatus());

        return this.page(pageDomain.toPlusPage(), queryWrapper);
    }

    public List<UserInfo> userLogin(String username) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        // 支持手机号或者用户账号登录
        queryWrapper.eq(UserInfo::getUserAccount, username)
                .or().eq(UserInfo::getPhoneNumber, username);
        return this.list(queryWrapper);
    }

    public List<UserInfo> checkUserAccountAndPhone(String userAccount, String phoneNumber) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        // 支持手机号或者用户账号登录
        queryWrapper.eq(UserInfo::getUserAccount, userAccount)
                .or().eq(UserInfo::getPhoneNumber, phoneNumber);
        return this.list(queryWrapper);
    }

    public List<RoleInfo> selectUserRole(Long id) {
        return this.baseMapper.selectUserRole(id);
    }

    /**
     * 创建用户
     *
     * @param userInfo
     * @return
     */
    public UserInfo createUserInfo(UserInfo userInfo) {
        // 创建用户 用户账户 和 手机号不能相同
        List<UserInfo> userInfos = this.checkUserAccountAndPhone(userInfo.getUserAccount(), userInfo.getPhoneNumber());
        if (CollUtil.isNotEmpty(userInfos)) {
            throw new BusinessException("用户账号或手机号已存在");
        }

        // 密码需要加密
        userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));

        // 保存用户信息
        this.save(userInfo);

        return userInfo;
    }
}
